/*
   Copyright 2010 the original author or authors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package com.googlecode.flightreports.flightlist;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.googlecode.kohmori.springmvc.SimpleReportController;
import javax.servlet.http.HttpSession;

public class ReportController extends SimpleReportController
{
    @Override
    protected Object formBackingObject(HttpServletRequest arg0) throws Exception {
        ReportParams form = (ReportParams) super.formBackingObject(arg0);
//      form.setCreateSourceXMLFile(true);  // debugging only
        return form;        
    }

    @Override
    protected Map referenceData(HttpServletRequest request) throws ServletException {
        Map<String, List> refData = new HashMap<String, List>();
        ArrayList<String> sortOptions = ReportParams.getSortOptions();
        refData.put("sortOptions", sortOptions);
        
        // obtain DB connection and parameter information
        HttpSession session = request.getSession(true);
        conn = (Connection) session.getAttribute("connection");

        String sqlStatement = "select distinct airline_name " +
                "from APP.V_FLIGHTLIST";

        ArrayList<String> airlineList = new ArrayList<String>();
        try {
            PreparedStatement pstmt = conn.prepareStatement(sqlStatement);            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                airlineList.add(rs.getString("airline_name"));
            }
            rs.close();
            pstmt.close();
            refData.put("airlineList", airlineList);
        } catch (SQLException e) {
            throw new ServletException("refData error: " + e.getMessage());
        }
               
        return refData;
    }
}
